package com.example.app;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.app.HomePage;
import com.example.app.adapter.MMAdapter;
import com.example.app.communication.MissionCheck;
import com.example.app.data.MMissionBean;
import com.example.app.database.DBMissionData;

import java.util.ArrayList;
import java.util.List;

public class MMFragment1 extends Fragment implements ListView.OnItemClickListener {

    private List<MMissionBean> data;
    private DBMissionData missionData;
    private View rootView;
    private int listCount;
    private TextView textView;
    private ListView listView;
    public MMAdapter mmAdapter;

    public MMFragment1() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     *
     * @return A new instance of fragment MMFragment1.
     */
    // TODO: Rename and change types and number of parameters
    public static MMFragment1 newInstance() {
        MMFragment1 fragment = new MMFragment1();
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initBean();

    }

    private void initBean() {
        data=new ArrayList<>();
        missionData= new DBMissionData(getContext());
        data=missionData.getAllData();
        listCount=data.size();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(rootView==null)
        {
            rootView=inflater.inflate(R.layout.mm_fragment1, container, false);
        }
        initView();
        return rootView;
    }

    private void initView() {
        textView=rootView.findViewById(R.id.mm_caution);
        listView=rootView.findViewById(R.id.mm_lv);
        mmAdapter=new MMAdapter(data,getContext(),listCount);
        if(listCount<1)
            textView.setVisibility(View.VISIBLE);
        else textView.setVisibility(View.INVISIBLE);
        listView.setAdapter(mmAdapter);
        listView.setOnItemClickListener(this);
    }



    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent=new Intent(getActivity(),MMCheck.class);
        intent.putExtra("missionNumber",data.get(i).getMissionNumber());
        startActivity(intent);
        getActivity().onBackPressed();
    }

}